package L619;

import java.util.Arrays;

/**
 * @program: javase
 * @description: 逆时针旋转90度
 * @author: luolidong
 * @create: 2021-06-19 16:52
 * @version: 1.0
 */
public class Demo3 {
    public void rotate(int[][] matrix) {
        int len1 = matrix.length;
        if (len1 == 1) {
            return;
        }
        // 以水平中轴线翻转
        for (int i = 0; i < len1; i++) {
            for (int j = 0; j < len1/2; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[i][len1 - j - 1];
                matrix[i][len1 - j - 1] = temp;
            }
        }
        for (int[] a : matrix) {
            System.out.println(Arrays.toString(a));
        }
        System.out.println();
        // 以对角线翻转
        for (int i = 0; i < len1 ; i++) {
            for (int j = 0; j<i;j++) {
                // 注意这里只需翻转一角的，否则会覆盖
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
    }

    public static void main(String[] args) {
        int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        new Demo3().rotate(arr);
        for (int[] a : arr) {
            System.out.println(Arrays.toString(a));
        }

    }
}
